package com.lin.missyou.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Calendar;

/**
 * 订单工具类
 * @author gcq
 * @Create 2021-11-14
 */
@Component
public class OrderUtil {

    private static String[] yearCodes;

    @Value("${missyou.year-codes}")
    public String[] setYearCodes(String yearCodes) {
        return OrderUtil.yearCodes = yearCodes.split(",");
    }

    /**
     * 生成订单号
     *
     * @return
     */
    public static String makeOrderNo() {
        StringBuffer joiner = new StringBuffer();
        Calendar calendar = Calendar.getInstance();
        //毫秒时间
        String mills = String.valueOf(calendar.getTimeInMillis());
        //
        String micro = LocalDateTime.now().toString();
        // 两位随机数
        String random = String.valueOf(Math.random() * 1000).substring(0, 2);
        joiner.append(OrderUtil.yearCodes[calendar.get(Calendar.YEAR) - 2021])
                .append(Integer.toHexString(calendar.get(Calendar.MONTH) + 1).toUpperCase())
                .append(calendar.get(Calendar.DAY_OF_MONTH))
                .append(mills.substring(mills.length() - 5, mills.length()))
                .append(micro.substring(micro.length() - 3, micro.length()))
                .append(random);
        return joiner.toString();
    }
}